package com.wfs.suanfa.leet;

/**
 * 数组实现队列
 */
public class ArrayQueue {
    //数组
    private String[] items;
    private int n=0;
    //头尾下标
    private int head=0;
    private int tail=0;

    //申请一个大小为capacity数组
    public ArrayQueue(int capacity){
        items=new String[capacity];
        n=capacity;
    }
    //入队
    public boolean enqueue(String item){
        if (tail==n) {
            if (head==0)
                return false;
            for (int i=head;i<tail;i++){
                items[i-head]=items[i];
            }
            tail-=head;
            head=0;
        }
        items[tail]=item;
        ++tail;
        return true;
    }

    /**
     * 出队
     */
    public String dequeue(String item){
        if (head==tail) return null;
        String item1 = items[head];
        ++head;
        return item1;
    }
}
